package com.itservice.uzalarm;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class Top extends Activity implements OnClickListener{
	
	private TextView tv1,tv2,tv3,tv4,tv5,wtv1,wtv2,wtv3,wtv4,wtv5;
	private CheckBox cb1,cb2,cb3,cb4,cb5;
	private Button bt_save;
	
	private SharedPreferences pref_mng,pref1,pref2,pref3,pref4,pref5;
	private SharedPreferences.Editor prefmng_editor;
	private int ON_OFF_FLAG;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.top);

        /* Create Pref */
        createPref();
        
        /* Load Pref */
        loadPref();

/*------View Setting------*/        
        /* First WackUp date */
        tv1 = (TextView)findViewById(R.id.TextView01);
        tv1.setOnClickListener(this);

        tv2 = (TextView)findViewById(R.id.TextView02);
        tv2.setOnClickListener(this);

        tv3 = (TextView)findViewById(R.id.TextView03);
        tv3.setOnClickListener(this);
        
        tv4 = (TextView)findViewById(R.id.TextView04);
        tv4.setOnClickListener(this);
        
        tv5 = (TextView)findViewById(R.id.TextView05);
        tv5.setOnClickListener(this);

        wtv1 = (TextView)findViewById(R.id.WeekData_top1);
        wtv1.setOnClickListener(this);

        wtv2 = (TextView)findViewById(R.id.WeekData_top2);
        wtv2.setOnClickListener(this);

        wtv3 = (TextView)findViewById(R.id.WeekData_top3);
        wtv3.setOnClickListener(this);
        
        wtv4 = (TextView)findViewById(R.id.WeekData_top4);
        wtv4.setOnClickListener(this);
        
        wtv5 = (TextView)findViewById(R.id.WeekData_top5);
        wtv5.setOnClickListener(this);

        
        cb1 = (CheckBox) findViewById(R.id.check01);
        cb1.setOnClickListener(this);
        
        cb2 = (CheckBox) findViewById(R.id.check02);
        cb2.setOnClickListener(this);
        
        cb3 = (CheckBox) findViewById(R.id.check03);
        cb3.setOnClickListener(this);
        
        cb4 = (CheckBox) findViewById(R.id.check04);
        cb4.setOnClickListener(this);
        
        cb5 = (CheckBox) findViewById(R.id.check05);
        cb5.setOnClickListener(this);
        
        ImageView iv5 = new ImageView(this); 
        iv5 = (ImageView)findViewById(R.id.ImageView05);
        iv5.setImageResource(R.drawable.path);

        ImageView iv6 = new ImageView(this); 
        iv6 = (ImageView)findViewById(R.id.ImageView06);
        iv6.setImageResource(R.drawable.path);

        ImageView iv7 = new ImageView(this); 
        iv7 = (ImageView)findViewById(R.id.ImageView07);
        iv7.setImageResource(R.drawable.path);
        
        ImageView iv8 = new ImageView(this); 
        iv8 = (ImageView)findViewById(R.id.ImageView08);
        iv8.setImageResource(R.drawable.path);

        ImageView iv9 = new ImageView(this); 
        iv9 = (ImageView)findViewById(R.id.ImageView09);
        iv9.setImageResource(R.drawable.path);

        bt_save = (Button)findViewById(R.id.Buttonx);
        bt_save.setOnClickListener(this);
 /*------View Setting------*/        
        ViewReset();
    }
    
    @Override
    public void onPause(){
    	super.onPause();
    	
    }
    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent tIntent)
    {
    	/* Result Code is? */
    	if (resultCode == RESULT_OK)
   		{
    		String tmpString = tIntent.getStringExtra("RET_KEY");
            /* Get Data from DataManager */
    		/* Or Call ViewReset()  */
    		switch(key2num(tmpString)){
    			case 1:
    				tv1.setText(getMyTime(pref1));
    		        wtv1.setText(pref1.getString(Utility.SWEEKLY_CONF_KEY, ""));
    		        break;
    			case 2:
    				tv2.setText(getMyTime(pref2));
    		        wtv2.setText(pref2.getString(Utility.SWEEKLY_CONF_KEY, ""));
    		        break;
    			case 3:
    				tv3.setText(getMyTime(pref3));
    		        wtv3.setText(pref3.getString(Utility.SWEEKLY_CONF_KEY, ""));
    		        break;
    			case 4:	
    				tv4.setText(getMyTime(pref4));
    		        wtv4.setText(pref4.getString(Utility.SWEEKLY_CONF_KEY, ""));
    		        break;
    			case 5:
    				tv5.setText(getMyTime(pref5));
    		        wtv5.setText(pref5.getString(Utility.SWEEKLY_CONF_KEY, ""));
    		        break;
    			default:
    				break;
    		}
   		}
   		else
   		{
   		}
    }

	@Override
	public void onClick(View v) {
		/* textView */
		if(v == tv1 || v == wtv1){
			nextAct(Utility.PREF_DATA1_KEY,com.itservice.uzalarm.Main.class);
		}else if(v == tv2 || v == wtv2){
			nextAct(Utility.PREF_DATA2_KEY,com.itservice.uzalarm.Main.class);
		}else if(v == tv3 || v == wtv3){
			nextAct(Utility.PREF_DATA3_KEY,com.itservice.uzalarm.Main.class);
		}else if(v == tv4 || v == wtv4){
			nextAct(Utility.PREF_DATA4_KEY,com.itservice.uzalarm.Main.class);
		}else if(v == tv5 || v == wtv5){
			nextAct(Utility.PREF_DATA5_KEY,com.itservice.uzalarm.Main.class);
		}
		/* CheckBox */
		else if(v == cb1){
			if(cb1.isChecked() == true){
				ON_OFF_FLAG = stateCheckisOn(ON_OFF_FLAG,Utility.DATA1_ON);
			}else{
				ON_OFF_FLAG = stateCheckisOff(ON_OFF_FLAG,Utility.DATA1_ON);
			}
		}else if(v == cb2){
			if(cb2.isChecked() == true){
				ON_OFF_FLAG = stateCheckisOn(ON_OFF_FLAG,Utility.DATA2_ON);
			}else{
				ON_OFF_FLAG = stateCheckisOff(ON_OFF_FLAG,Utility.DATA2_ON);
			}
		}else if(v == cb3){
			if(cb3.isChecked() == true){
				ON_OFF_FLAG = stateCheckisOn(ON_OFF_FLAG,Utility.DATA3_ON);
			}else{
				ON_OFF_FLAG = stateCheckisOff(ON_OFF_FLAG,Utility.DATA3_ON);
			}
		}else if(v == cb4){
			if(cb4.isChecked() == true){
				ON_OFF_FLAG = stateCheckisOn(ON_OFF_FLAG,Utility.DATA4_ON);
			}else{
				ON_OFF_FLAG = stateCheckisOff(ON_OFF_FLAG,Utility.DATA4_ON);
			}
		}else if(v == cb5){
			if(cb5.isChecked() == true){
				ON_OFF_FLAG = stateCheckisOn(ON_OFF_FLAG,Utility.DATA5_ON);
			}else{
				ON_OFF_FLAG = stateCheckisOff(ON_OFF_FLAG,Utility.DATA5_ON);
			}
		}
		/* Save Button Event */
		else if(v == bt_save){
			prefmng_editor.putInt(Utility.ON_OFF_KEY, ON_OFF_FLAG);
			prefmng_editor.commit();
			
			/* Start Service */
			Intent sintent = new Intent(Top.this,UzalarmService.class);
			startService(sintent);
			
			/*[FIX ME] 
			 *  Button押下でActivity Down?
			 */
			finish();
		}
	}
	
	private boolean isChecked(int data, int mask){
		boolean ret = false;
		if((data & mask) == mask){
			ret = true;
		}
		return ret;
	}
	
	private int stateCheckisOn(int data, int mask){
		return (data | mask);
	}
	private int stateCheckisOff(int data, int mask){
		return (data & ~mask);
	}
	
	private void createPref(){
        pref_mng = getSharedPreferences(Utility.PREF_MANAGER_KEY,Context.MODE_PRIVATE);
		prefmng_editor = pref_mng.edit();

        prefmng_editor.putString(Utility.DATA1_NAME_KEY,Utility.PREF_DATA1_KEY);
        prefmng_editor.putString(Utility.DATA2_NAME_KEY,Utility.PREF_DATA2_KEY);
        prefmng_editor.putString(Utility.DATA3_NAME_KEY,Utility.PREF_DATA3_KEY);
        prefmng_editor.putString(Utility.DATA4_NAME_KEY,Utility.PREF_DATA4_KEY);
        prefmng_editor.putString(Utility.DATA5_NAME_KEY,Utility.PREF_DATA5_KEY);
        prefmng_editor.commit();
	}

	
	private void loadPref(){
		ON_OFF_FLAG = pref_mng.getInt(Utility.ON_OFF_KEY, 0x00000000);
		pref1 = getSharedPreferences(Utility.PREF_DATA1_KEY,Context.MODE_PRIVATE);
		pref2 = getSharedPreferences(Utility.PREF_DATA2_KEY,Context.MODE_PRIVATE);
		pref3 = getSharedPreferences(Utility.PREF_DATA3_KEY,Context.MODE_PRIVATE);
		pref4 = getSharedPreferences(Utility.PREF_DATA4_KEY,Context.MODE_PRIVATE);
		pref5 = getSharedPreferences(Utility.PREF_DATA5_KEY,Context.MODE_PRIVATE);
	}
	
	private String getMyTime(SharedPreferences sunPref){
		String ret = "";
		int hour = sunPref.getInt(Utility.WAKE_HOUR_KEY, -1);
		int minute = sunPref.getInt(Utility.WAKE_MIN_KEY, -1);

		/* Create Time String */
		if( hour >=0 && hour < 10){
			ret = "0";
		}
		ret = ret + Integer.toString(hour) + ":";
		if(minute >=0 && minute < 10){
			ret = ret + "0";
		}
		ret = ret + Integer.toString(minute);
		if(ret.equals("-1:-1")){
			ret = "NO DATA";
		}
		return ret;
	}
	
	void nextAct(String strKey,Class<?> clz){
		Intent intent = new Intent(this, clz);
		intent.putExtra("INPUT_KEY",strKey);
		startActivityForResult(intent, 0);
	}
	
    private int key2num(String key){
    	int ret = 0;
    	if(key.equals(Utility.PREF_DATA1_KEY)){
    		ret = 1;
    	}else if(key.equals(Utility.PREF_DATA2_KEY)){
    		ret = 2;
    	}else if(key.equals(Utility.PREF_DATA3_KEY)){
    		ret = 3;
    	}else if(key.equals(Utility.PREF_DATA4_KEY)){
    		ret = 4;
    	}else if(key.equals(Utility.PREF_DATA5_KEY)){
    		ret = 5;
    	}
    	return ret;
    }
    
	private void ViewReset(){
		/* TextView */
		tv1.setText(getMyTime(pref1));
        tv2.setText(getMyTime(pref2));
        tv3.setText(getMyTime(pref3));
        tv4.setText(getMyTime(pref4));
        tv5.setText(getMyTime(pref5));
        wtv1.setText(pref1.getString(Utility.SWEEKLY_CONF_KEY, ""));
        wtv2.setText(pref2.getString(Utility.SWEEKLY_CONF_KEY, ""));
        wtv3.setText(pref3.getString(Utility.SWEEKLY_CONF_KEY, ""));
        wtv4.setText(pref4.getString(Utility.SWEEKLY_CONF_KEY, ""));
        wtv5.setText(pref5.getString(Utility.SWEEKLY_CONF_KEY, ""));
        
        /* CheckBox */
        cb1.setChecked(isChecked(ON_OFF_FLAG,Utility.DATA1_ON));
        cb2.setChecked(isChecked(ON_OFF_FLAG,Utility.DATA2_ON));
        cb3.setChecked(isChecked(ON_OFF_FLAG,Utility.DATA3_ON));
        cb4.setChecked(isChecked(ON_OFF_FLAG,Utility.DATA4_ON));
        cb5.setChecked(isChecked(ON_OFF_FLAG,Utility.DATA5_ON));
	}
}